<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="订单ID" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入订单ID" />
                </el-form-item>
                <el-form-item label="用户ID" prop="userId">
                    <el-input v-model="formData.userId" placeholder="请输入用户ID" />
                </el-form-item>
                <el-form-item label="订单金额" prop="totalAmount">
                    <el-input v-model="formData.totalAmount" placeholder="请输入订单金额" />
                </el-form-item>
                <el-form-item label="订单状态" prop="orderStatus">
                    <el-radio-group v-model="formData.orderStatus" placeholder="请选择订单状态[1已支付2未支付3支付中]">
                        <el-radio label="0">请选择字典生成</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="支付方式" prop="payChannel">
                    <el-input v-model="formData.payChannel" placeholder="请输入支付方式[1支付宝2微信3银联4其它]" />
                </el-form-item>
                <el-form-item label="创建日期和时间" prop="orderDate">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.orderDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择订单创建日期和时间"
                    />
                </el-form-item>
                <el-form-item label="商品id" prop="productId">
                    <el-input v-model="formData.productId" placeholder="请输入该订单对应的商品id" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  ordersEdit, ordersAdd, ordersDetail } from '@/api/orders'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑订单' : '新增订单'
})

const formData = reactive({
    id: '',
    userId: '',
    totalAmount: '',
    orderStatus: '',
    payChannel: '',
    orderDate: '',
    productId: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入订单ID',
            trigger: ['blur']
        }
    ],
    userId: [
        {
            required: true,
            message: '请输入用户ID',
            trigger: ['blur']
        }
    ],
    totalAmount: [
        {
            required: true,
            message: '请输入订单金额',
            trigger: ['blur']
        }
    ],
    orderStatus: [
        {
            required: true,
            message: '请选择订单状态[1已支付2未支付3支付中]',
            trigger: ['blur']
        }
    ],
    payChannel: [
        {
            required: true,
            message: '请输入支付方式[1支付宝2微信3银联4其它]',
            trigger: ['blur']
        }
    ],
    orderDate: [
        {
            required: true,
            message: '请选择订单创建日期和时间',
            trigger: ['blur']
        }
    ],
    productId: [
        {
            required: true,
            message: '请输入该订单对应的商品id',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await ordersEdit(data) : await ordersAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await ordersDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
